#!/user/bin/perl -w
#-w ?
# global variables
$menu_choice = "";
$cd_file_name  = "list.cbd";
$cdtitle     = "";
$cdtype      = "";
$cdac        = "";
$cnt         = 0;
@cdlist      = ();

#$SIG{INT} = sub { write_file(); exit; };

system("touch $cd_file_name") unless ( -f $cd_file_name );

# read cd title list from file
read_in();
#system("clear");
print "\nMiniCD manager\n";

#sleep(2);

while (1) {
	set_menu_choice();
	if    ( $menu_choice =~ /^a$/i ) { add_records(); }
	elsif ( $menu_choice =~ /^r$/i ) { remove_cd(); }
	elsif ( $menu_choice =~ /^f$/i ) { find_cd("y"); }
	elsif ( $menu_choice =~ /^u$/i ) { update_cd(); }
	elsif ( $menu_choice =~ /^c$/i ) { count_cds(); }
	elsif ( $menu_choice =~ /^l$/i ) { list_cds(); }
	elsif ( $menu_choice =~ /^q$/i ) {
		print "Do you write cd list to file? \n";
		 if(get_confirm()) {
		 	write_file();
		 }
		 last;
	}
	else { print "Sorry, choice not recognized .\n"; }
}

#write_file();
exit;

sub read_in {
	open CDLIST, $cd_file_name
	  or die "Could not open $cd_file_name $!\n";  #$! chua thong bao loi cua open
	while (<CDLIST>) {
		chomp;
		next if $_=~/^#/;
		push @cdlist, $_;
	}
	close CDLIST;
}

sub write_file {
	open CDLIST, ">" . $cd_file_name
	  or die "Could not write to $cd_file_name : $!\n";
	#system("rm -f $cd_file_name");
	print CDLIST "# My cd\n";
	print CDLIST "# Cd list\n";
	foreach (@cdlist) {
		print CDLIST "$_\n";
	}
	close CDLIST;
}

# ham doc du lieu tu ban phim
sub get_return {
	print "\npress return";
	<>    #doc mot dong trang tu STDIN va bo qua
}

sub get_confirm {
	print "Are you sure? ";
	while (1) {
		chomp( $_ = <> );    # doc ket qua do nguoi dung nhap vao va luu vao $_
		return 1 if ( $_ eq "y" );    # 1 la true, 0 la false ?
		if ( $_ eq "n" ) {
			print "Cancelled!\n";
			return 0;
		}
		print "Please enter yes or no.\n";
	}
}

# hien thi main menu va yeu cau nguoi dung chon
# su dung ki thuat tai lieu here cua shell. Linh in se in toan bo noi dung

sub set_menu_choice {
	#print `clear`;    # cach goi lenh he thong bang cap nhay nguoc

	print "  a) Add new CD title\n";
	print "  l) List CD title\n";
	print "  c) Count list CD\n";
	print "  f) Find CD\n";

	print "  r) Remove cd \n";
	print "  u) Update cd\n";

	print "  q) Quit \n\n";
	print "Please enter choise then press return\n";
	chomp( $menu_choice = <> );
	return;
}

sub add_records {
	input();

	print "About to add a new entry\n";
	print "$cdtitle $cdtype $cdac\n";

	if ( get_confirm() ) {
		insert_title( $cdtitle, $cdtype, $cdac );
	}
}

sub insert_title {
	push @cdlist, ( join ",", @_ );
	print "\ninserted:", ( join ",", @_ );
	++$cnt;
}

sub find_cd {
	print "Enter string for search: ";
	chomp( $searchstr = <> );
	return 0 unless $searchstr;

	my $match = 0;
	foreach (@cdlist) {
		if ( $_ =~ /$searchstr/i ) {
			print "$_\n";
			$match = 1;
		}
	}

	if ( $match == 0 ) {
		print "\nNot found\n";
	}
	get_return();
}

#dem so CD trong mang
sub count_cds {
	print "Found " . ( scalar @cdlist ) . " CDs, ";
	get_return();
}

sub list_cds {
	foreach (@cdlist) {
		print "$_\n";
	}
	get_return();
}

sub remove_cd {
	if ( scalar @cdlist == 0 ) {
		print "No cd";
		return;
	}
	my $i = 0;
	foreach (@cdlist) {
		++$i;
		print "$i. $_\n";
	}

	print "Enter cd number: ";
	chomp( $searchstr = <> );
	--$searchstr;

	my @tmp = @cdlist;
	@cdlist = ();
	$i      = 0;

	foreach (@tmp) {
		if ( $i ne $searchstr ) {
			$cdlist[$i] = $tmp[$i];
		}
	}
	--$cnt;
	get_return();
}

sub update_cd {
	my $i = 0;
	foreach (@cdlist) {
		++$i;
		print "$i. $_\n";
	}

	print "Enter cd number: ";
	chomp( $searchstr = <> );
	--$searchstr;

	input();

	print "About update info: \n";
	print "$cdtitle $cdtype $cdac\n";
	if ( get_confirm() ) {
		$cdlist[$searchstr] = "$cdtitle,$cdtype,$cdac";
	}
	else {
		print "No update";
	}

	get_return();
}

sub input {
	print "Enter cd title:  ";
	chomp( $cdtitle = <> );
	$cdtitle =~ s/,.*//;

	print "Enter type: ";
	chomp( $cdtype = <> );
	$cdtype =~ s/,.*//;

	print "Enter artist/composer: ";
	chomp( $cdac = <> );
	$cdac =~ s/,.*//;
}
